home *** CD-ROM | disk | FTP | other *** search
/ FM Towns: Software Contest 3 / FM Towns Software Contest 3.iso / exp / astral / a1 / game / source / wire3d_c.c < prev   
C/C++ Source or Header  |  1994-01-07  |  3KB  |  150 lines

  1. // "wire3d_c.c"
  2. // ワイヤーフレームによる物体の表現(C言語版)
  3.  
  4. #include "wire3d.h"
  5. #include <stdio.h>
  6. #include "sin8.c"
  7.  
  8. void wire3d(ANGLE *angle,int *offset,WIRE_OBJECT *obj,int page)
  9. {
  10.     int i;
  11.     int point_num_m3,point_work[MAX_POINT*3];
  12.     int line_num_m2;
  13.     int x1,y1,x2,y2,w,xx,yy;
  14.     int point_w[4][3],z_w,pass[MAX_POINT*3];
  15.     
  16.     // 回転,オフセット付加,透視変換
  17.     point_num_m3=obj->point_num*3;
  18.     for(i=0;i<point_num_m3;i+=3){
  19.         // Y軸の回転
  20.         point_w[1][0]=( obj->point[i+0] * cos_data[angle[1]] -
  21.          obj->point[i+2] * sin_data[angle[1]] )>>7;
  22.         point_w[1][2]=( obj->point[i+0] * sin_data[angle[1]] +
  23.          obj->point[i+2] * cos_data[angle[1]] )>>7;
  24.         
  25.         // X軸の回転
  26.         point_w[2][1]=( obj->point[i+1] * cos_data[angle[0]] +
  27.          point_w[1][2] * sin_data[angle[0]])>>7;
  28.         point_w[3][2]=((-obj->point[i+1] * sin_data[angle[0]] +
  29.          point_w[1][2] * cos_data[angle[0]])>>7)+offset[2];
  30.         
  31.         // Z軸の回転
  32.         point_w[3][0]=(( point_w[1][0] * cos_data[angle[2]] +
  33.          point_w[2][1] * sin_data[angle[2]])>>7)+offset[0];
  34.         point_w[3][1]=((-point_w[1][0] * sin_data[angle[2]] +
  35.          point_w[2][1] * cos_data[angle[2]])>>7)+offset[1];
  36.         
  37.         // 透視変換
  38.         pass[i]=0;
  39.         z_w=point_w[3][2]+0x100;
  40.         if(z_w<=0){
  41.             pass[i]=1;
  42.             continue;
  43.         }
  44.         point_work[i+0]=(( point_w[3][0]<<8)/z_w)+128;
  45.         point_work[i+1]=((-point_w[3][1]<<8)/z_w)+120;
  46.     }
  47.     
  48.     // クリッピング,描画
  49.     line_num_m2=obj->line_num*2;
  50.     for(i=0;i<line_num_m2;i+=2){
  51.         if(pass[obj->line[i]*3]==1 || pass[obj->line[i+1]*3]==1)continue;
  52.         
  53.         x1=point_work[obj->line[i  ]*3  ];
  54.         y1=point_work[obj->line[i  ]*3+1];
  55.         x2=point_work[obj->line[i+1]*3  ];
  56.         y2=point_work[obj->line[i+1]*3+1];
  57.         
  58.         if(x1>0 && x1<255 && y1>0 && y1<255 &&
  59.          x2>0 && x2<255 && y2>0 && y2<255){
  60.             line(x1,y1,x2,y2,obj->color,page);
  61.             continue;
  62.         }
  63.         if( (x1<0 && x2<0) || (x1>255 && x2>255) ||
  64.          (y1<0 && y2<0) || (y1>255 && y2>255) ){
  65.             continue;
  66.         }
  67.          
  68.         if(x2<0){
  69.             xx=1;
  70.         }
  71.         else{
  72.             if(x2>255){
  73.                 xx=255;
  74.             }
  75.             else{
  76.                 xx=x2;
  77.             }
  78.         }
  79.         if(x2==x1){
  80.             yy=y2;
  81.         }
  82.         else{
  83.             yy=y2-(y2-y1)*(x2-xx)/(x2-x1);
  84.         }
  85.         if(yy<0){
  86.             yy=0;
  87.         }
  88.         else{
  89.             if(yy>255){
  90.                 yy=255;
  91.             }
  92.             else{
  93.                 goto EXIT1;
  94.             }
  95.         }
  96.         if(y2==y1){
  97.             xx=x2;
  98.         }
  99.         else{
  100.             xx=x2-(x2-x1)*(y2-yy)/(y2-y1);
  101.         }
  102.         EXIT1:
  103.         x2=xx;
  104.         y2=yy;
  105.         
  106.          w=x2;x2=x1;x1=w;
  107.          w=y2;y2=y1;y1=w;
  108.           
  109.         if(x2<0){
  110.             xx=1;
  111.         }
  112.         else{
  113.             if(x2>255){
  114.                 xx=255;
  115.             }
  116.             else{
  117.                 xx=x2;
  118.             }
  119.         }
  120.         if(x2==x1){
  121.             yy=y2;
  122.         }
  123.         else{
  124.             yy=y2-(y2-y1)*(x2-xx)/(x2-x1);
  125.         }
  126.         if(yy<0){
  127.             yy=0;
  128.         }
  129.         else{
  130.             if(yy>255){
  131.                 yy=255;
  132.             }
  133.             else{
  134.                 goto EXIT2;
  135.             }
  136.         }
  137.         if(y2==y1){
  138.             xx=x2;
  139.         }
  140.         else{
  141.             xx=x2-(x2-x1)*(y2-yy)/(y2-y1);
  142.         }
  143.         EXIT2:
  144.         x2=xx;
  145.         y2=yy;
  146.         
  147.          line(x1,y1,x2,y2,obj->color,page);
  148.     }
  149. }
  150.